package org.nimi317.web_gis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.nimi317.web_gis.Enum.JobEnum;
import org.nimi317.web_gis.exception.E;
import org.nimi317.web_gis.exception.RException;
import org.nimi317.web_gis.dao.JobMapper;
import org.nimi317.web_gis.entity.Job;
import org.nimi317.web_gis.service.IJobService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author thunderobot
 */
@Service
public class IJobServiceImpl extends ServiceImpl<JobMapper, Job> implements IJobService {
    @Override
    public List<Job> getAllJobs() {
        LambdaQueryWrapper<Job> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Job::getStatus, JobEnum.Running);
        return this.list(wrapper);
    }

    @Override
    public Job getJobById(Integer id) {
        return this.getById(id);
    }

    @Override
    public void start(Integer id) {
        if (this.getById(id).getStatus().equals(JobEnum.Running)) {
            return;
//            throw new RException(E.JobAlreadyRunningException);
        }
        Job job = new Job();
        job.setId(id);
        job.setStatus(JobEnum.Running);
        this.updateById(job);
    }

    @Override
    public void stop(Integer id) {
        if (this.getById(id).getStatus().equals(JobEnum.Stopped)) {
            throw new RException(E.JobAlreadyStoppedException);
        }
        Job job = new Job();
        job.setId(id);
        job.setStatus(JobEnum.Stopped);
        this.updateById(job);
    }
}
